package com.my.loginbmob.view.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.my.loginbmob.R;
import com.my.loginbmob.constant.MyConstant;
import com.my.loginbmob.dialog.LoadingDialog;
import com.my.loginbmob.presenter.IRegistePresenter;
import com.my.loginbmob.presenter.RegistPresenter;
import com.my.loginbmob.view.iview.IRegisteView;

import cn.bmob.v3.Bmob;

public class RegistActivity extends AppCompatActivity implements View.OnClickListener, IRegisteView {
    private EditText etUserName, etPassword, etPasswordAgin;
    private Button btnRegiste;
    private IRegistePresenter iRegistePresenter;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        initView();
        initListener();
    }

    private void initView() {
        etUserName = findViewById(R.id.et_user_name_regist);
        etPassword = findViewById(R.id.et_password_regist);
        etPasswordAgin = findViewById(R.id.et_password_agin);
        btnRegiste = findViewById(R.id.btn_register);
        loadingDialog = new LoadingDialog(this);
        // new接口
        iRegistePresenter = new RegistPresenter(this);
        // 初始化Bmob
        Bmob.initialize(this, MyConstant.MY_CONSTANT_BMOB_APPLICATION_ID);
    }

    private void initListener() {
        btnRegiste.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register: // 注册
                setProgressVisibility(1);
                iRegistePresenter.checkInput(etUserName.getText().toString(),
                        etPassword.getText().toString(), etPasswordAgin.getText().toString());
                break;
        }
    }

    @Override
    public void setErroToast(String msg) {
        toastUtil(msg);
    }

    /**
     * 提示框
     */
    private void toastUtil(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setProgressVisibility(int visibility) {
        if (visibility == 1) { // 显示加载框
            loadingDialog.show();
        } else { // 结束加载框
            loadingDialog.dismiss();
        }
    }

    @Override
    public void finishActivity() {
        finish();
    }

}
